# 1、读取文件
# with语句会自动关闭文件
with open(file="../../data/student.txt", encoding="utf-8", mode="r") as f:
    lines = f.readlines()

# 统计年龄的人数
age_num = {}
for line in lines:
    # 取出年龄
    age = line.strip().split(",")[2]
    # 如果存在加1,如果不存在存入一个1
    if age in age_num:
        age_num[age] += 1
    else:
        age_num[age] = 1
print(age_num)

# 3、将统计结果保存到文件中
with open(file="../../data/age_num.txt", encoding="utf-8", mode="w") as f:
    # 循环将数据保存到文件中
    for age,num in age_num.items():
        f.write(f"{age},{num}\n")